<?php

class myUser extends sfGuardSecurityUser
{
  public $Member = null;

  public function initialize (sfEventDispatcher $dispatcher, sfStorage $storage, $options = array())
  {
    parent::initialize($dispatcher, $storage, $options);
    
    sfContext::getInstance()->getLogger()
      ->info('Credentials of user: ' . print_r($this->getCredentials(), true));
    
    if ($this->isMember())
    {
      $this->Member = $this->getProfile()->Member;
    }
  }

  public function getMember ()
  {
    return $this->getProfile()
      ->getMember();
  }

  public function parseCredentials ()
  {
    foreach ($this->credentials as $k => $credential)
    {
      $this->credentials[$k] = preg_replace_callback('/(\&(.*)\&)+/U', array(
        $this, 'getCredentialPlaceholderValueCallback'), $credential);
    }
  }

  public function getCredentialPlaceholderValueCallback ($matches)
  {
    return $this->getCredentialPlaceholderValue($matches[2]);
  }

  public function getCredentialPlaceholderValue ($key)
  {
    $value = '';
    switch ($key)
    {
      case 'unit_id':
        $value = $this->getOrganizationalUnitId();
        break;
    }
    
    return $value;
  }

  public function signIn ($user, $remember = false, $con = null)
  {
    $result = parent::signIn($user, $remember, $con);
    
    $credentials_from_commissions = $this->getGuardUser()
      ->retrieveCredentialsFromCommissions();
    $this->addCredentials($credentials_from_commissions);
    
    $this->parseCredentials();
    sfContext::getInstance()->getLogger()
      ->info('Credentials of user: ' . print_r($this->getCredentials(), true));
    
    $this->addCredential(sprintf('user_id_%s', $this->getGuardUser()
      ->getId()));
    
    return $result;
  }

  public function getMemberId ()
  {
    if ($this->isMember())
    {
      return $this->Member->getId();
    }
    else
    {
      return 0;
    }
  }

  /**
   * @return boolean
   */
  public function isMember ()
  {
    //return true; // TODO adjust
    if (is_null($this->getProfile()))
    {
      return false; //throw new LogicException();
    }
    return $this->getProfile()
      ->getMemberId() > 0;
  }

  /**
   * @return integer
   */
  public function getOrganizationalUnitId ()
  {
    if ($id = $this->getProfile()
      ->getOwningOrganizationalUnitId())
    {
      return $id;
    }
    elseif ($this->isMember())
    {
      return $this->getMember()
        ->getOrganizationalUnitId();
    }
    else
    {
      throw new Exception();
    }
  }

  /**
   * @return OrganisationalUnit
   */
  public function getOrganizationalUnit ()
  {
    if ($this->getProfile()
      ->getOwningOrganizationalUnitId() > 0)
    {
      return $this->getProfile()
        ->getOrganizationalUnit();
    }
    elseif ($this->isMember())
    {
      return $this->getMember()
        ->getOrganizationalUnit();
    }
    else
    {
      throw new Exception();
    }
  }

  public function getOwningOrganizationalUnitIds ()
  {
    $parent = $this->getOrganizationalUnit();
    $ids = array(
      0 => $parent->getId());
    $units = $parent->getDescendants();
    foreach ($units as $unit)
    {
      $ids[] = $unit->getId();
    }
    return $ids;
  }

  public function getDisplayName ()
  {
    if ($this->isMember())
    {
      return $this->Member->getFirstName() . ' ' . $this->Member->getLastName();
    }
    else
    {
      return $this->getGuardUser()
        ->getUsername();
    }
  }

  public function isAuthenticated ()
  {
    return parent::isAuthenticated();
  
   #return true; // TODO Adjust for production
  }

  //	public function getGuardUser() {
//		return sfGuardUserTable::getInstance()->findOneByUsername('System');
//	}
}
